/**
 * 
 */
package de.cbf.cam.messagecenter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Nebelritter
 *
 */
public class MessageCenter {
	
	private static final MessageCenter instance = new MessageCenter();
	
	public static final int TYPE_ALL  		= 1<<1;
	public static final int TYPE_TIME  		= 1<<2;
	public static final int TYPE_CHARACTER 	= 1<<3;
	public static final int TYPE_SKILL		= 1<<4;	
	
	/**
	 * 
	 */
	public MessageCenter() {
		
	}
	
	public static MessageCenter getInstance(){
		return instance;
	}
	
	private int notifyTimeMessage(Message value){
		int result = 0;
		for (IMessageListener listener : timeMessageListeners) {
			result = result + listener.recieveMessage(value);
		}
		return result;
	}
	
	private int notifyCharacterMessage(Message value){
		int result = 0;
		for (IMessageListener listener : characterMessageListeners) {
			result = result + listener.recieveMessage(value);
		}
		return result;
	}
	
	private int notifySkillMessage(Message value){
		int result = 0;
		for (IMessageListener listener : skillMessageListeners) {
			result = result + listener.recieveMessage(value);
		}
		return result;
	}
	
	public int issueMessage(Message value){
		int type = value.getType();
		if((type | TYPE_ALL) != 0){//if all types are requested notify all
			type = TYPE_TIME|TYPE_CHARACTER|TYPE_SKILL;
		}
		int result = 0;
		if ((type | TYPE_TIME)!= 0) {
			//is of time type -> notify time listeners
			result = result + notifyTimeMessage(value); 
		}
		if ((type | TYPE_CHARACTER)!= 0) {
			//is of character type -> notify character listeners
			result = result + notifyCharacterMessage(value); 
		}
		if ((type | TYPE_SKILL)!= 0) {
			//is of skill type -> notify skill listeners
			result = result + notifySkillMessage(value); 
		}		
		return result;
	}
	
	public boolean registerMessageListener(int type,IMessageListener listener){
		boolean change = false;
		switch (type) {
			case TYPE_ALL:{
				change = change && registerTimeMessageListener(listener);				
				change = change && registerCharacterMessageListener(listener);				
				change = change && registerSkillMessageListener(listener);
				break;
			}		
			case TYPE_TIME:{
				change = change && registerTimeMessageListener(listener);
				break;
			}	
			case TYPE_CHARACTER:{
				change = change && registerCharacterMessageListener(listener);
				break;
			}	
			case TYPE_SKILL:{
				change = change && registerSkillMessageListener(listener);
				break;
			}	
		}		
		return change;
	}
	
	public boolean deRegisterMessageListener(int type,IMessageListener listener){
		boolean change = false;
		switch (type) {
			case TYPE_ALL:{
				change = change && deRegisterTimeMessageListener(listener);
				change = change && deRegisterCharacterMessageListener(listener);
				change = change && deRegisterSkillMessageListener(listener);
				break;
			}	
			case TYPE_TIME:{
				change = change && deRegisterTimeMessageListener(listener);
				break;
			}	
			case TYPE_CHARACTER:{
				change = change && deRegisterCharacterMessageListener(listener);
				break;
			}	
			case TYPE_SKILL:{
				change = change && deRegisterSkillMessageListener(listener);
				break;
			}	
		}		
		return change;
	}
	
	
	private Collection<IMessageListener> timeMessageListeners = new ArrayList<IMessageListener>();
	public boolean registerTimeMessageListener(IMessageListener listener){
		return timeMessageListeners.add(listener);
	}
	public boolean deRegisterTimeMessageListener(IMessageListener listener){
		return timeMessageListeners.remove(listener);
	}

	private Collection<IMessageListener> characterMessageListeners = new ArrayList<IMessageListener>();
	public boolean registerCharacterMessageListener(IMessageListener listener){
		return characterMessageListeners.add(listener);
	}
	public boolean deRegisterCharacterMessageListener(IMessageListener listener){
		return characterMessageListeners.remove(listener);
	}
	
	private Collection<IMessageListener> skillMessageListeners = new ArrayList<IMessageListener>();
	public boolean registerSkillMessageListener(IMessageListener listener){
		return skillMessageListeners.add(listener);
	}
	public boolean deRegisterSkillMessageListener(IMessageListener listener){
		return skillMessageListeners.remove(listener);
	}
}
